#!/usr/bin/env python
# coding=utf-8
"""
@Author  : youjia - 卞志伟
@file    : os_util.py
@contact : bianzhiwei@iyoujia.com
@time    : 2019-08-21 10:30 
@Desc    : 
@Software: PyCharm
"""
import os
import sys

# 当前文件的路径
pwd, filename = os.path.split(os.path.abspath(__file__))
# 当前文件的父路径
father_path = os.path.abspath(os.path.dirname(pwd) + os.path.sep + ".")
# 当前文件的前两级目录
grader_father = os.path.abspath(os.path.dirname(pwd) + os.path.sep + "..")
sys.path.append(pwd)
sys.path.append(father_path)
sys.path.append(grader_father)

import zipfile
from report_system.utils.log_util import log


def all_files_path(files_path, file_start):
    """
    遍历问文件夹 files_path 下所有 file_start 开头的文件的全路径并放入list中返回
    :param files_path:
    :param file_start:
    :return:
    """
    result = []  # 所有的文件
    for top, dirs, files in os.walk(files_path):
        log.info("该目录为: {} ".format(top))
        log.info("该目录下包含的所有文件夹: {} ".format(dirs))
        log.info("该目录下包含的所有文件: {} ".format(files))
        for file in files:
            if file.startswith(file_start):
                # 合并成一个完整路径
                file_complete_path = os.path.join(top, file)
                result.append(file_complete_path)
    return result


def zip_files(files_path: list or str, zip_name: str):
    """
    压缩文件
    :param files_path:
    :param zip_name:
    :return:
    """
    if isinstance(files_path, str):
        files_path = [files_path]
    # 路径应该是 ../report_system/report_files
    path = father_path + '/report_files/'
    if not os.path.exists(path):
        os.makedirs(path)
    # 合成zip文件全路径
    zip_path = path + zip_name + '.zip'
    # 穿件zip文件
    file_zip = zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED)
    for file in files_path:
        log.info("添加压缩文件: {} ".format(file))
        # 添加压缩文件
        file_zip.write(file)
    file_zip.close()
    log.info("文件压缩成功: {} ".format(zip_path))
    return zip_path


def rename(old_name, new_name, old_path='', new_path=''):
    """
    修改文件名
    :param old_name:
    :param new_name:
    :param old_path:
    :param new_path:
    :return:
    """
    old_file = os.path.join(old_path, old_name)
    new_file = os.path.join(new_path, new_name)

    os.rename(old_file, new_file)

    return new_file


def separate_path(path):
    """
    分离路径
    :param path:
    :return:
    """
    # 获取路径
    abs_path, abs_name = os.path.dirname(path), os.path.basename(path)
    return abs_path, abs_name


def get_cur_file_name(file_obj):
    return os.path.basename(file_obj).replace('.py','')


if __name__ == '__main__':
    pass
    a = os.path.join('', 'ss.name')
    print(a)
    # files = all_files_path("/Users/youjia/fsdownload/", "airbnb_tax_1_2019-8")  # 文件的位置，多个文件用“，”隔开
    # zip_name = 'airbnb账单'  # 压缩包名字
    # zip_files(files, zip_name)
